/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.visibility.rule;

import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;

import com.sinai.mshab.visibility.VisibilityRule;

import java.util.HashSet;
import java.util.Set;

/**
 * The <code>ShowIfEqualText</code> {@link VisibilityRule} is a rule that allows
 * a child widget to be shown when the parent {@link TextBoxBase} widget is set
 * to some text.
 * 
 * @author Arthur Kalmenson
 */
public class ShowIfEqualText implements
		VisibilityRule<TextBoxBase, Widget, String> {

	/**
	 * The parent widget.
	 */
	private TextBoxBase parent;

	/**
	 * The child widget.
	 */
	private Widget child;

	/**
	 * The set of valuest hat would trigger for child to be show.
	 */
	private Set<String> values = new HashSet<String>();

	/**
	 * Creates a new <code>ShowIfEqualText</code> with a parent widget, a child
	 * widget and the value that would show said child widget.
	 * 
	 * @param parent
	 *            the parent widget.
	 * @param value
	 *            one trigger that would show the child widget.
	 * @param child
	 *            the child widget.
	 */
	public ShowIfEqualText(TextBoxBase parent, String value, Widget child) {

		if (parent == null || value == null || child == null) {
			throw new IllegalArgumentException("Constructor arguments to "
					+ this.getClass().getName() + " cannot be null.");
		}

		this.parent = parent;
		this.child = child;
		values.add(value);

		// add the keyboard listener to the parent widget.
		parent.addKeyboardListener(new TextEqualsListener());
	}

	public void execute() {
		if (values.contains(parent.getText())) {
			child.setVisible(true);
		} else {
			child.setVisible(false);
		}
	}

	public void setParentWidget(TextBoxBase widget) {
		if (widget == null) {
			throw new IllegalArgumentException(getClass().getName()
					+ ": Parent widget cannot be null.");
		}

		parent = widget;
		parent.addKeyboardListener(new TextEqualsListener());
	}

	public TextBoxBase getParentWidget() {
		return parent;
	}

	public void setChildWidget(Widget widget) {
		if (widget == null) {
			throw new IllegalArgumentException(getClass().getName()
					+ ": Child widget cannot be null.");
		}

		child = widget;
	}

	public Widget getChildWidget() {
		return child;
	}

	public void addTrigger(String value) {
		if (value == null) {
			throw new IllegalArgumentException(getClass().getName()
					+ ": Trigger cannot be null.");
		}
		values.add(value);
	}

	public Set<String> getTriggers() {
		return values;
	}

	private class TextEqualsListener implements KeyboardListener {

		public void onKeyDown(Widget arg0, char arg1, int arg2) {
			// do nothing
		}

		public void onKeyPress(Widget arg0, char arg1, int arg2) {
			execute();
		}

		public void onKeyUp(Widget arg0, char arg1, int arg2) {
			// do nothing
		}
	}
}
